# ------------------------------------ #
# Virtual Machine Definition Functions #
# ------------------------------------ #

# Export virtual machine definitions
function DefinitionExport-VirtualMachines([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.virtualMachines))) {return}
	foreach ($virtualMachine in $subscription.virtualMachines.virtualMachine)
	{
	    if (!(Should-Run($virtualMachine))) {continue}
		DefinitionExport-VirtualMachineInstance $virtualMachine
    }
}

# Remove virtual machine definitions
function DefinitionRemove-VirtualMachines([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.virtualMachines))) {return}
	foreach ($virtualMachine in $subscription.virtualMachines.virtualMachine)
	{
	    if (!(Should-Run($virtualMachine))) {continue}
		DefinitionRemove-VirtualMachineInstance $virtualMachine
    }
}

# Import virtual machine definitions
function DefinitionImport-VirtualMachines([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.virtualMachines))) {return}
	foreach ($virtualMachine in $subscription.virtualMachines.virtualMachine)
	{
	    if (!(Should-Run($virtualMachine))) {continue}
		DefinitionImport-VirtualMachineInstance $virtualMachine
    }
}

# Export virtual machine instance definition
function DefinitionExport-VirtualMachineInstance($virtualMachine)
{
	if (Get-AzureVM -ServiceName $virtualMachine.service -Name $virtualMachine.name -WarningAction Ignore)
	{
		Write-Information "Exporting virtual machine '$($virtualMachine.name)' definition ... " -NoNewLine $true
        if (!(Test-Path -LiteralPath "$env:dp0\Export" -PathType Container))
        {
            New-Item -ItemType Directory -Path "$env:dp0\Export" | Out-Null
        }
	    $file = "$env:dp0\Export\$($virtualMachine.service)_$($virtualMachine.name).xml"
	    $operation = Export-AzureVM -ServiceName $virtualMachine.service -Name $virtualMachine.name -Path $file
	    if (!$? -or $operation.OperationStatus -ne "Succeeded") { throw "Virtual machine '$($virtualMachine.name)' operation failed." }
		Write-Text "done."
	}
	else
	{
		Write-Information "Exporting virtual machine '$($virtualMachine.name)' definition ... skipped."
	}
}

# Remove virtual machine instance definition
function DefinitionRemove-VirtualMachineInstance($virtualMachine)
{
	if (Get-AzureVM -ServiceName $virtualMachine.service -Name $virtualMachine.name -WarningAction Ignore)
	{
		Write-Information "Removing virtual machine '$($virtualMachine.name)' definition ... " -NoNewLine $true
        Remove-VirtualMachineInstanceConfiguration $virtualMachine $false
		Write-Text "done."
	}
	else
	{
		Write-Information "Removing virtual machine '$($virtualMachine.name)' definition ... skipped."
	}
}

# Import virtual machine instance definition
function DefinitionImport-VirtualMachineInstance($virtualMachine)
{
	if ((Get-AzureVM -ServiceName $virtualMachine.service -Name $virtualMachine.name -WarningAction Ignore) -eq $null)
	{
		Write-Information "Importing virtual machine '$($virtualMachine.name)' definition ... " -NoNewLine $true
        $file = "$env:dp0\Export\$($virtualMachine.service)_$($virtualMachine.name).xml"
	    $operation = Import-AzureVM -Path $file | New-AzureVM -ServiceName $virtualMachine.service -VNetName $virtualMachine.network.name -WarningAction Ignore
	    if (!$? -or $operation.OperationStatus -ne "Succeeded") { throw "Virtual machine '$($virtualMachine.name)' operation failed." }
		Write-Text "done."
	}
	else
	{
		Write-Information "Importing virtual machine '$($virtualMachine.name)' definition ... skipped."
	}
}